package com.graduate.daydayup.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.graduate.daydayup.R;
import com.graduate.daydayup.base.BaseActivity;
import com.graduate.daydayup.fragment.index.IndexFragment;
import com.graduate.daydayup.fragment.lesson.LessonFragment;
import com.graduate.daydayup.fragment.mine.MineFragment;


public class MainActivity extends BaseActivity {
    private int[] idArrays = new int[]{R.id.tv_home, R.id.tv_lesson, R.id.mine};

    private FragmentTransaction beginTransaction;
    private IndexFragment homeFragment;
    private LessonFragment lessonFragment;
    private MineFragment mineFragment;
    private TextView tvHome;
    private TextView tvLesson;
    private TextView tvMine;

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public String setTitle() {
        return null;
    }

    @Override
    public String setTextRight() {
        return "";
    }


    @Override
    public void setImgRightListener() {

    }

    @Override
    public boolean isAddToolbar() {
        return false;
    }

    @Override
    public void initViewIds() {
        tvHome = findViewById(R.id.tv_home);
        tvLesson = findViewById(R.id.tv_lesson);
        tvMine = findViewById(R.id.mine);

    }

    @Override
    public void initView() {
        setFragment(0);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        requestPermission();
    }

    @Override
    public void initListener() {
        super.initListener();
        tvHome.setOnClickListener(v -> {
            setFragment(0);
        });
        tvLesson.setOnClickListener(v -> {
            setFragment(1);
        });
        tvMine.setOnClickListener(v -> {
            setFragment(2);
        });
    }

    /**
     * 设置Fragment
     *
     * @param index: 下标
     */
    private void setFragment(int index) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        beginTransaction = fragmentManager.beginTransaction();
        hideFragment();
        switch (index) {
            case 0:
                //首页
                if (homeFragment == null) {
                    homeFragment = new IndexFragment();
                    beginTransaction.add(R.id.fl_layout, homeFragment);
                } else {
                    beginTransaction.show(homeFragment);
                }
                break;
            case 1:
                //课程
                if (lessonFragment == null) {
                    lessonFragment = new LessonFragment();
                    beginTransaction.add(R.id.fl_layout, lessonFragment);
                } else {
                    beginTransaction.show(lessonFragment);
                }
                break;
            case 2:
                //发现
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    beginTransaction.add(R.id.fl_layout, mineFragment);
                } else {
                    beginTransaction.show(mineFragment);
                }
                break;

        }
        beginTransaction.commitAllowingStateLoss();
        refreshBottomStatus(index);
    }

    /**
     * 改变底部图片和文字的状态
     *
     * @param position: 下标
     */
    private void refreshBottomStatus(int position) {
        for (int i = 0; i < idArrays.length; i++) {
            findViewById(idArrays[i]).setSelected(position == i);
        }
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideFragment() {
        if (homeFragment != null)
            beginTransaction.hide(homeFragment);
        if (lessonFragment != null)
            beginTransaction.hide(lessonFragment);
        if (mineFragment != null)
            beginTransaction.hide(mineFragment);
    }

    @SuppressLint("WrongConstant")
    private void requestPermission(){
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();


    }
}
